import _ from './utils/common'
import $ from './utils/request'
App({

    // 全局变量
    globalData: {
        isIphoneX: false,
        isShowLuckydraw: false
    },

    onShow: function(ops) {

        let vm = this;
        
        // 获取群分享信息
        if (ops.scene == 1036 || ops.scene == 1044) {
            this.globalData.shareTickets = ops.shareTicket;
        } else {
            delete this.globalData.shareTickets;
        }

        // 判断是否是iPhone X系列
        wx.getSystemInfo({
            success(res) {

                let model = res.model;
                if (model.indexOf('iPhone X') >= 0) {
                    vm.globalData.isIphoneX = true;
                }

            }
        })

    },

    /**
     * 显示成功提示框
     */
    showSuccess(msg, callback) {
        wx.showToast({
        title: msg,
        icon: 'success',
        success() {
            callback && (setTimeout(() => {
            callback();
            }, 1500));
        }
        });
    },

    /**
     * 显示失败提示框
     */
    showError(msg, callback) {
        wx.showModal({
        title: '友情提示',
        content: msg,
        showCancel: false,
        success(res) {
            callback && callback();
        }
        });
    },

    /**
     * 执行用户登录
     */
    doLogin() {
        // 保存当前页面
        let pages = getCurrentPages();
        if (pages.length) {
            let currentPage = pages[pages.length - 1];
            "pages/login/login" != currentPage.route && wx.setStorageSync("currentPage", currentPage);
        }
        // 跳转授权页面
        wx.navigateTo({
            url: "/pages/login/login"
        });
    },

    /**
     * 验证登录
     */
    checkIsLogin() {
        return wx.getStorageSync('token') != '' && wx.getStorageSync('userId') != '';
    },

    // 登录授权
    getAuth(e, callback) {
        let vm = this;

        if (!e.detail.userInfo) return;
        wx.login({
            success: function(res) {
                if (res.code) {

                    let params = {};
                    params.code = res.code;
                    wx.getUserInfo({
                        lang:"zh_CN",
                        success: function(gres) {
                            params.encryptedData = gres.encryptedData;
                            params.iv = gres.iv;
                            params.rawData = gres.rawData
                            params.signature = gres.signature
                            $.ajax({
                                url: '/api/wx_auth/register',
                                method: 'post',
                                data: params
                            }).then(function(res){
                                console.log(res)
                          
                                if (res.code === 0) {
                                    // 记录token user_id
                                    wx.setStorageSync('token', res.token);
                                    wx.setStorageSync('userId', res.userId);

                                    vm.showSuccess('授权成功');
                                    
                                    setTimeout(function() {
                                        callback ? callback() : '';
                                    }, 1000)
                                } else if (data.code === 500) {
                                    vm.showError('网络繁忙，请重新登陆');

                                } else if (data.code === 401) {
                                    vm.showError(data.msg);
                                    
                                }
                            })

                        }
                    })

                } else {
                   vm.showError(res.errMsg);
                }
            }
        })

    }
    
})